using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;
using System.Reflection;
using System.Collections.Generic;

namespace SP2010.Enhancements.SEO
{  

    [Guid("5e22ddb3-31e9-41bd-b531-b4f3cca2c18f")]
    public class Search_Engine_OptimizationEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            string str = WebConfigHttpModules.DNVBA_TrimPageHTML_HttpModule.ToString();
            string fullName = typeof(SP2010.Enhancements.SEO.SEOHttpHandler).FullName;
            string name = "add[@name='" + str + "']";
            string xpath = "configuration/system.webServer/modules";
            string str5 = "<add name=\"" + str + "\" type=\"" + fullName + ", " + Assembly.GetAssembly(typeof(SP2010.Enhancements.SEO.SEOHttpHandler)).FullName + "\" />";
            this.ModifyWebConfig(properties, name, xpath, str5, 0, SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode);
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            this.RemoveAllModificationsWebConfig(properties);
        }

        public void ModifyWebConfig(SPFeatureReceiverProperties properties, string name, string xpath, string value, uint sequence, SPWebConfigModification.SPWebConfigModificationType webConfigModificationType)
        {
            string dNVBAHttpModules = this.DNVBAHttpModules;
            SPWebService contentService = SPWebService.ContentService;
            SPWebConfigModification item = new SPWebConfigModification(name, xpath)
            {
                Value = value,
                Sequence = sequence,
                Type = webConfigModificationType,
                Owner = dNVBAHttpModules
            };
            contentService.WebConfigModifications.Add(item);
            contentService.Update();
            contentService.ApplyWebConfigModifications();
        }

        public void RemoveAllModificationsWebConfig(SPFeatureReceiverProperties properties)
        {
            string dNVBAHttpModules = this.DNVBAHttpModules;
            SPWebService contentService = SPWebService.ContentService;
            List<SPWebConfigModification> list = new List<SPWebConfigModification>();
            foreach (SPWebConfigModification modification in contentService.WebConfigModifications)
            {
                if (modification.Owner.ToLower() == dNVBAHttpModules.ToLower())
                {
                    list.Add(modification);
                }
            }
            foreach (SPWebConfigModification modification in list)
            {
                contentService.WebConfigModifications.Remove(modification);
            }
            contentService.Update();
            contentService.ApplyWebConfigModifications();
        }

        private string DNVBAHttpModules
        {
            get
            {
                return "DNVBAHttpModules";
            }
        }

        private enum WebConfigHttpModules
        {
            DNVBA_TrimPageHTML_HttpModule
        }
    }
}
